var addressbookCtl = require('../controller/controller.addressbook');
var companyCtl = require('../controller/controller.company');
var addressbookDeptCtl = require('../controller/controller.addressbookDept');
var industryCtl = require('../controller/controller.industry');
var async = require('async');
module.exports = {

    //保存联系人
    editAddressbook: function (req, res, next) {
        var params = req.body;
        if (params == null) {
            res.redirect('/error');
            return;
        }
        var that  = this;
        params.createUid = req.session.uid;
        //新增或查询公司
        companyCtl.create({"name":params.company,"createUid":params.createUid}, function (err, result) {
            if (err) {
                res.redirect('/error');
                return;
            } else {
                params.company =  result.obj.rowId;
                if(params.department != ''){
                    addressbookDeptCtl.create({"name":params.department,"createUid":params.createUid,'enable':'1'}, function (err, result) {
                        if (err) {
                            res.redirect('/error');
                            return;
                        } else {
                            params.department =  result.obj.rowId;
                            that.saveOrUpdateAddressbook(params,res,next);
                        }
                    });
                }else{
                    that.saveOrUpdateAddressbook(params,res,next);
                }
            }
        });



    },
    saveOrUpdateAddressbook:function(params,res,next){
        if (params.action != 'add') {
            addressbookCtl.update(params, function (err, result) {
                if (err) {
                    res.redirect('/error');
                    return;
                } else {
                    next(result);
                }
            });
        } else {
            addressbookCtl.create(params, function (err, result) {
                if (err) {
                    res.redirect('/error');
                    return;
                } else {
                    next(result);
                }
            });
        }
    },
    //公司联想
    companySearch:function(req, res, next){
        var params = req.query;
        if (params == null) {
            res.redirect('/error');
            return;
        }
        companyCtl.getListByName(params,function(err,result){
            if (err) {
                res.redirect('/error');
                return;
            } else {
                var jsonData = [];
                if(result != null ){
                    result.obj.forEach(function(data){
                        var res = {};
                        res.value = data.name.toString();
                        res.data = data.name.toString();
                        jsonData.push(res)
                    })
                }else{
                    jsonData.push({ "value": "暂无相关公司信息", "data": "暂无相关公司信息" })
                }

                next({
                    "query":params.query,
                    "suggestions":jsonData
                });
            }
        })
    },
    //角色联想
    deptSearch:function(req, res, next){
        var params = req.query;
        if (params == null) {
            res.redirect('/error');
            return;
        }
        addressbookDeptCtl.getListByName(params,function(err,result){
            if (err) {
                res.redirect('/error');
                return;
            } else {
                var jsonData = [];
                if(result != null ){
                    result.obj.forEach(function(data){
                        var res = {};
                        res.value = data.name.toString();
                        res.data = data.name.toString();
                        jsonData.push(res)
                    })
                }
                next({
                    "query":params.query,
                    "suggestions":jsonData
                });
            }
        })
    },
    //删除联系人信息
    del: function (req, res, next) {

        var params = req.body;
        if (params == null) {
            res.redirect('/error');
            return;
        }
        params.enable = '0';
        addressbookCtl.update(params, function (err, result) {
            if (err) {
                res.redirect('/error');
                return;
            } else {
                next(result);
            }

        })

    },
    //获取联系人信息
    getAddressbookInfo: function (req, res, next) {
        var params = req.query;
        addressbookCtl.getInfo(params, function (err, result) {
            if (err) {
                res.redirect('/error');
                return;
            }
            next({
                'addressbook': result.obj,
            });

        })
    },


    //获取联系人详情
    getDetail: function (req, res, next) {
        var params = req.query;
        var action = params.action;
        if (action == 'add') {
            async.series({
                industryList: function (callback) {
                    industryCtl.getIndustryList(null, function (err, result) {
                        if (err) {
                            handleError(res, err);
                        } else {
                            callback(null, result);
                        }
                    })
                },
            }, function (err, results) {
                if (err) {
                    handleError(res, err);
                } else {
                    next({
                        'addressbook': null,
                        'action': 'add',
                        'industryList': results.industryList.obj
                    });
                }
            })

        } else if (action == 'detail') {
            async.series({
                industryList: function (callback) {
                    industryCtl.getIndustryList(null, function (err, result) {
                        if (err) {
                            handleError(res, err);
                        } else {
                            callback(null, result);
                        }
                    })
                },
                addressbook: function (callback) {
                    addressbookCtl.getInfo(params, function (err, result) {
                        if (err) {
                            handleError(res, err);
                        } else {
                            callback(null, result);
                        }
                    })
                },
            }, function (err, results) {
                if (err) {
                    handleError(res, err);
                } else {
                    next({
                        'addressbook': results.addressbook.obj,
                        'action': 'detail',
                        'industryList': results.industryList.obj
                    });
                }
            })
        }

    },

    //获取通讯录列表
    getAddressbookList: function (req, res, next) {

        var params = req.body;
        if (params.queryStr == null) {
            params.queryStr = "";
        }

        var role = req.session.userinfo.role;

        companyCtl.getList(params, function (err, result) {
            if (err) {
                res.redirect('/error');
                return;
            }
            next({'list': result.obj});
        });

    },


};
